/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.SignatureBy;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Signature;
/*     */ import com.zimbra.qa.unittest.prov.Names;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvSignature
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  52 */     provUtil = new LdapProvTestUtil();
/*  53 */     prov = provUtil.getProv();
/*  54 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  59 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart) throws Exception {
/*  63 */     return createAccount(localPart, null);
/*     */   }
/*     */   
/*     */   private Account createAccount(String localPart, Map<String, Object> attrs) throws Exception
/*     */   {
/*  68 */     return provUtil.createAccount(localPart, domain, attrs);
/*     */   }
/*     */   
/*     */   private void deleteAccount(Account acct) throws Exception {
/*  72 */     provUtil.deleteAccount(acct);
/*     */   }
/*     */   
/*     */   private void deleteSignature(Account acct, Signature signature) throws Exception {
/*  76 */     String signatureId = signature.getId();
/*  77 */     prov.deleteSignature(acct, signatureId);
/*  78 */     signature = prov.get(acct, Key.SignatureBy.id, signatureId);
/*  79 */     Assert.assertNull(signature);
/*     */   }
/*     */   
/*     */   private Signature createSignatureRaw(Account acct, String signatureName) throws Exception
/*     */   {
/*  84 */     Map<String, Object> attrs = new HashMap();
/*  85 */     return prov.createSignature(acct, signatureName, attrs);
/*     */   }
/*     */   
/*     */   private Signature createSignature(Account acct, String signatureName) throws Exception
/*     */   {
/*  90 */     Signature signature = prov.get(acct, Key.SignatureBy.name, signatureName);
/*  91 */     Assert.assertNull(signature);
/*     */     
/*  93 */     createSignatureRaw(acct, signatureName);
/*     */     
/*  95 */     signature = prov.get(acct, Key.SignatureBy.name, signatureName);
/*  96 */     Assert.assertNotNull(signature);
/*  97 */     Assert.assertEquals(signatureName, signature.getName());
/*     */     
/*  99 */     return signature;
/*     */   }
/*     */   
/*     */   private Account getFresh(Account acct) throws Exception {
/* 103 */     prov.flushCache(CacheEntryType.account, null);
/* 104 */     return prov.get(Key.AccountBy.id, acct.getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createSignature() throws Exception {
/* 109 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 110 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 112 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 113 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/*     */     
/* 115 */     Assert.assertEquals(acct.getId(), signature.getAccount().getId());
/*     */     
/* 117 */     deleteSignature(acct, signature);
/* 118 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createSignatureAlreadyExists() throws Exception {
/* 123 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 124 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 126 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 127 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/*     */     
/* 129 */     boolean caughtException = false;
/*     */     try {
/* 131 */       createSignatureRaw(acct, SIGNATURE_NAME);
/*     */     }
/*     */     catch (AccountServiceException e) {
/* 134 */       if ("account.SIGNATURE_EXISTS".equals(e.getCode())) {
/* 135 */         caughtException = true;
/*     */       }
/*     */     }
/* 138 */     Assert.assertTrue(caughtException);
/*     */     
/* 140 */     deleteSignature(acct, signature);
/* 141 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void modifySignature() throws Exception {
/* 146 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 147 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 149 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 150 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/*     */     
/* 152 */     Map<String, Object> attrs = new HashMap();
/* 153 */     String MODIFIED_ATTR_NAME = "zimbraPrefMailSignature";
/* 154 */     String MODIFIED_ATTR_VALUE = "modifySignature";
/* 155 */     attrs.put(MODIFIED_ATTR_NAME, MODIFIED_ATTR_VALUE);
/* 156 */     prov.modifySignature(acct, signature.getId(), attrs);
/*     */     
/* 158 */     acct = getFresh(acct);
/* 159 */     signature = prov.get(acct, Key.SignatureBy.name, SIGNATURE_NAME);
/* 160 */     Assert.assertEquals(MODIFIED_ATTR_VALUE, signature.getAttr(MODIFIED_ATTR_NAME));
/*     */     
/* 162 */     deleteSignature(acct, signature);
/* 163 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void renameSignature() throws Exception {
/* 168 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 169 */     String SIGNATURE_NAME_ON_ACCOUNT_ENTRY = Names.makeSignatureName(genSignatureName("sig-on-account-entry"));
/* 170 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 172 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 173 */     Signature signatureOnAccountEntry = createSignature(acct, SIGNATURE_NAME_ON_ACCOUNT_ENTRY);
/* 174 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 179 */     Map<String, Object> attrs = new HashMap();
/*     */     
/* 181 */     String MODIFIED_ATTR_NAME = "zimbraSignatureName";
/* 182 */     String NEW_SIGNATURE_NAME = Names.makeSignatureName(genSignatureName("sig-on-account-entry-new"));
/* 183 */     String MODIFIED_ATTR_VALUE = NEW_SIGNATURE_NAME;
/* 184 */     attrs.put(MODIFIED_ATTR_NAME, MODIFIED_ATTR_VALUE);
/* 185 */     prov.modifySignature(acct, signatureOnAccountEntry.getId(), attrs);
/*     */     
/* 187 */     acct = getFresh(acct);
/* 188 */     signatureOnAccountEntry = prov.get(acct, Key.SignatureBy.name, NEW_SIGNATURE_NAME);
/* 189 */     Assert.assertEquals(MODIFIED_ATTR_VALUE, signatureOnAccountEntry.getAttr(MODIFIED_ATTR_NAME));
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 194 */     attrs = new HashMap();
/*     */     
/* 196 */     MODIFIED_ATTR_NAME = "zimbraSignatureName";
/* 197 */     NEW_SIGNATURE_NAME = Names.makeSignatureName(genSignatureName("new"));
/* 198 */     MODIFIED_ATTR_VALUE = NEW_SIGNATURE_NAME;
/* 199 */     attrs.put(MODIFIED_ATTR_NAME, MODIFIED_ATTR_VALUE);
/* 200 */     prov.modifySignature(acct, signature.getId(), attrs);
/*     */     
/* 202 */     acct = getFresh(acct);
/* 203 */     signature = prov.get(acct, Key.SignatureBy.name, NEW_SIGNATURE_NAME);
/* 204 */     Assert.assertEquals(MODIFIED_ATTR_VALUE, signature.getAttr(MODIFIED_ATTR_NAME));
/*     */     
/* 206 */     deleteSignature(acct, signatureOnAccountEntry);
/* 207 */     deleteSignature(acct, signature);
/* 208 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAllSignatures() throws Exception {
/* 213 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 214 */     String SIGNATURE_NAME_1 = Names.makeSignatureName(genSignatureName("1"));
/* 215 */     String SIGNATURE_NAME_2 = Names.makeSignatureName(genSignatureName("2"));
/* 216 */     String SIGNATURE_NAME_3 = Names.makeSignatureName(genSignatureName("3"));
/*     */     
/* 218 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 219 */     Signature signature1 = createSignature(acct, SIGNATURE_NAME_1);
/* 220 */     Signature signature2 = createSignature(acct, SIGNATURE_NAME_2);
/* 221 */     Signature signature3 = createSignature(acct, SIGNATURE_NAME_3);
/*     */     
/* 223 */     acct = getFresh(acct);
/* 224 */     List<Signature> allSignatures = prov.getAllSignatures(acct);
/* 225 */     Assert.assertEquals(3L, allSignatures.size());
/*     */     
/* 227 */     Set<String> allSignatureIds = new HashSet();
/* 228 */     for (Signature signature : allSignatures) {
/* 229 */       allSignatureIds.add(signature.getId());
/*     */     }
/*     */     
/* 232 */     Assert.assertTrue(allSignatureIds.contains(signature1.getId()));
/* 233 */     Assert.assertTrue(allSignatureIds.contains(signature2.getId()));
/* 234 */     Assert.assertTrue(allSignatureIds.contains(signature3.getId()));
/*     */     
/* 236 */     deleteSignature(acct, signature1);
/* 237 */     deleteSignature(acct, signature2);
/* 238 */     deleteSignature(acct, signature3);
/* 239 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getSignature() throws Exception {
/* 244 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 245 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 247 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 248 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/* 249 */     String signatureId = signature.getId();
/*     */     
/* 251 */     acct = getFresh(acct);
/* 252 */     signature = prov.get(acct, Key.SignatureBy.id, signatureId);
/* 253 */     Assert.assertNotNull(signature);
/*     */     
/* 255 */     acct = getFresh(acct);
/* 256 */     signature = prov.get(acct, Key.SignatureBy.name, SIGNATURE_NAME);
/* 257 */     Assert.assertNotNull(signature);
/*     */     
/* 259 */     deleteSignature(acct, signature);
/* 260 */     deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createSignatureWithExistingId() throws Exception {
/* 265 */     String ACCT_NAME_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart());
/* 266 */     String SIGNATURE_NAME = Names.makeSignatureName(genSignatureName());
/*     */     
/* 268 */     Account acct = createAccount(ACCT_NAME_LOCALPART);
/* 269 */     Signature signature = createSignature(acct, SIGNATURE_NAME);
/* 270 */     String id = signature.getAttr("zimbraSignatureId");
/*     */     
/* 272 */     Map<String, Object> attrs = new HashMap();
/* 273 */     attrs.put("zimbraSignatureId", id);
/*     */     try {
/* 275 */       prov.createSignature(acct, SIGNATURE_NAME + "_NEW", attrs);
/* 276 */       Assert.fail("Create signature should not have succeeded, as signatureId exists.");
/*     */     } catch (AccountServiceException e) {
/* 278 */       Assert.assertEquals("account.SIGNATURE_EXISTS", e.getCode());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvSignature.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */